package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum PowerStatus implements EnumTrans<CompleteEntity.PowerStatus, PowerStatus> {
    /**
     * 默认属性
     * 唤醒
     * 休眠
     */
    PowerStatusDefault(CompleteEntity.PowerStatus.PowerStatusDefault),
    Wakeup(CompleteEntity.PowerStatus.Wakeup),
    Sleep(CompleteEntity.PowerStatus.Sleep),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.PowerStatus powerStatus;
    private static final long serialVersionUID = 987654321987654321L;

    PowerStatus(CompleteEntity.PowerStatus powerStatus) {
        this.id = powerStatus.getNumber();
        this.name = powerStatus.name();
        this.powerStatus = powerStatus;
    }

    /**
     * @param powerStatus
     * @return 对应的类型
     */
    public static PowerStatus getPowerStatusStruct(CompleteEntity.PowerStatus powerStatus) {
        Objects.requireNonNull(powerStatus, "powerStatus can not be null.");
        return Arrays.stream(PowerStatus.values())
                .filter(e -> e.name.equals(powerStatus.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.PowerStatus toProto() {
        return this.powerStatus;
    }

    @Override
    public PowerStatus formProto(CompleteEntity.PowerStatus powerStatus) {
        return getPowerStatusStruct(powerStatus);
    }
}

